package com.km.core.intercepter;

import lombok.extern.slf4j.Slf4j;
import okhttp3.*;

import java.io.IOException;
import java.util.UUID;

/**
 * <p>模拟数据返回</p>
 * Created by zhezhiyong@163.com on 2017/6/13.
 */
@Slf4j
public class NetInterceptor implements Interceptor {

    //    private static final String API_URL = "http://qxtsms.guodulink.net:8005/HttpQuickProcess/submitMessageAll";
    private static final String API_URL = "/HttpQuickProcess/submitMessageAll";

    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response;
        if (chain.request().url().toString().contains(API_URL)) {
            String msgId = UUID.randomUUID().toString().replaceAll("-", "");
//            {"time":"20170508103503","msgId":"17050810350326298","errorMsg":"","code":"0"}
//            BaseDto dto = new BaseDto();
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//            String time = sdf.format(new Date());
//            dto.put("time", time);
//            dto.put("msgId", msgId);
//            dto.put("errorMsg", "模拟数据返回");
//            dto.put("code", "0");
            String r = "00," + msgId;
            response = new Response.Builder()
                    .code(200)
                    .addHeader("Content-Type", "text/plain")
                    .body(ResponseBody.create(MediaType.parse("text/plain"), r))
                    .message(r)
                    .request(chain.request())
                    .protocol(Protocol.HTTP_2)
                    .build();
        } else {
            response = chain.proceed(chain.request());
        }
        return response;
    }
}
